<?php

namespace app\api\controller;

use think\Db;
use think\Exception;

class MessageActivity extends Base
{

    /**
     * 活动消息列表
     */
    public function getList()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (empty($param->memberId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '会员ID(memberId) 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [
            'member_id' => $param->memberId
        ];

        $model = db('message_activity');
        $fields = 'a.*,o.id as activityId,title_url,name,f_name,end_time';
        $order = 'create_date DESC';

        $list = $model->alias('a')
            ->where($where)
            ->join('activity o','a.activity_id = o.id')
            ->field($fields)
            ->limit($start, 100)
            ->order($order)
            ->select();

        if (!$list) {
            return show(config('status.ERROR_STATUS'), '没有消息', '没有消息');
        }
        $total = $model->where($where)->count();

        // 标记已读 TODO
        $result = $model->where($where)->update(['is_read' => 1]);
        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $list, $total);
    }
}